﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Cms.Entity.Article;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Cms.BLL.Article;
using Cms.BLL.Menu;

namespace Cms.Admin.Picture
{
    public partial class GroupList : PageList
    {
        private ArticleBO _article = new ArticleBO();

        private string _menuID;
        private string MenuID
        {
            get
            {
                if (_menuID == null)
                {
                    if (string.IsNullOrEmpty(Request["MenuID"]))
                    {
                        _menuID = (new MenuBO()).GetIDByCode(MenuCode).ToString();
                    }
                    else
                    {
                        _menuID = StrUtil.DecryptUrl(Request["MenuID"]);
                    }
                }
                return _menuID;
            }
        }

        

        protected string GetPicturePath(object path)
        {
            if (string.IsNullOrEmpty((string)path))
            {
                return GlobalVar.WebAppPath + "common/style/skin1/images/no_photo_s.png";
            }
            else
            {
                return (string)path;
            }
        }

        private string MenuCode
        {
            get
            {
                return Request["MenuCode"];
            }
        }

       // private PagerSortSetting _setting;

        protected void Page_Load(object sender, EventArgs e)
        {
            //_setting = this.Pager1.InitPager(new BindViewHandler(BindView));



            if (!IsPostBack)
            {
                
                BindView();
                if (MenuID != null)
                {
                    lblMenuName.Visible = true;
                    lblMenuName.Text = (new MenuBO()).GetName(MenuID) + "&nbsp;";
                }
            }
            AddAttr();
            //base.InitSelectID(hidSelectIDs);
            //strOrders = hidOrders.Value;
            hidOrders.Value = "";

            JSUtil.SetDivHeight("content", 70);
           
            //JSUtil.SetTableHeight("list", 145);

        }


        private void AddAttr()
        {        
            JSUtil.ShowForm(lnkAdd, "增加图片组", null, GlobalVar.WebCurrPath + "GroupEdit.aspx?MenuID=" + MenuID, 400, 205);
        }

        private void BindView()
        {


            IEnumerable<ArticleInfo> list = _article.GetList(int.Parse(MenuID), null, null);


            lblStatistic.Text = "[&nbsp;共<b style='color:red;'> " + list.Count() + "</b>&nbsp;组图片&nbsp;]";

            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataBind();


        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }
            List<int> lstSelectIDs = StrUtil.SplitToInt(strSelectIDs);

            //要删除相关的图片和附件
            _article.Delete(lstSelectIDs);
            BindView();
        }

        protected void btnSaveSequence_Click(object sender, EventArgs e)
        {
            //List<int> ids = new List<int>();
            //List<int> orders = new List<int>();
            //StrUtil.SplitIDOrders(ids, orders, strOrders);
            //_article.SaveOrders(ids, orders);
            //BindView();
        }


        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void drpPager_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindView();
        }


        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                string strID = StrUtil.EncryptUrl(lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString());
                HtmlAnchor lnkEdit = (HtmlAnchor)e.Item.FindControl("lnkEdit");
                HtmlAnchor lnkDelete = (HtmlAnchor)e.Item.FindControl("lnkDelete");

                string url = "GroupEdit.aspx?ID=" + strID + "&MenuID=" + MenuID;
                JSUtil.ShowForm(lnkEdit, "编辑图片组", null, url, 400, 205);


                lnkDelete.HRef = "Delete.ashx?FromUrl=GroupList&ArticleID=" + strID + "&MenuID=" + MenuID;

                lnkDelete.Attributes.Add("onclick", "return confirm('确认删除图片组?')");

            }
        }

    }
}
